package com.nut.spp.util;

import cn.hutool.http.HttpUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtil {

    public static void zipImages2(String zipFileName, String inputDirectory) throws IOException {
        File zipFile = new File(zipFileName);
        File inputDir = new File(inputDirectory);

        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile))) {
            for (File image : inputDir.listFiles()) {
                if (image.isFile()) {
                    FileInputStream fis = new FileInputStream(image);
                    ZipEntry zipEntry = new ZipEntry(image.getName());
                    zipOut.putNextEntry(zipEntry);

                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zipOut.write(bytes, 0, length);
                    }

                    zipOut.closeEntry();
                    fis.close();
                }
            }
        }
    }

    public static void zipImages(String zipFileName, String[] list) throws IOException {
        File zipFile = new File(zipFileName);

        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile))) {
//
//            for (String url : list) {
//
//                HttpUtil.download()
//
//            }
//            for (File image : inputDir.listFiles()) {
//                if (image.isFile()) {
//                    FileInputStream fis = new FileInputStream(image);
//                    ZipEntry zipEntry = new ZipEntry(image.getName());
//                    zipOut.putNextEntry(zipEntry);
//
//                    byte[] bytes = new byte[1024];
//                    int length;
//                    while ((length = fis.read(bytes)) >= 0) {
//                        zipOut.write(bytes, 0, length);
//                    }
//
//                    zipOut.closeEntry();
//                    fis.close();
//                }
//            }
        }
    }

    public static void main(String[] args) throws IOException {
//        try {
//            // 压缩目录下的所有图片
//            zipImages("images.zip", "E:\\demo");
//            System.out.println("图片压缩成功！");
//        } catch (IOException e) {
//            e.printStackTrace();
//            System.out.println("图片压缩失败！");
//        }

        String[] array = {"https://img.hwhhotels.com/165233334955632.jpg", "https://img.hwhhotels.com/165233334871874.jpg"};
        String[] names = {"a.jpg", "b.jpg"};

//        String url = "https://img.hwhhotels.com/165233334871874.jpg";

        String outFile = "E:\\aa.zip";

        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(outFile))) {
            for (int i = 0; i < array.length; i++) {
                ZipEntry zipEntry = new ZipEntry(names[i]);
                zipOut.putNextEntry(zipEntry);
                HttpUtil.download(array[i], zipOut, false);
                zipOut.closeEntry();
            }

        }

        System.out.println("done");
    }
}